// 云函数入口文件
const cloud = require('wx-server-sdk')

var crypto = require('crypto');
cloud.init()
//生成腾讯云ocr授权签名
var secretId = 'AKIDpVhefk4119L1WtQIL86Izm8PeZYSB4M5',
    secretKey = '69Y2OyWFqlmiXgEh4BIrByXO3FPoeR1m',
    appid = '1258450062',
    pexpired = 86400,
    userid = 0;

var now = parseInt(Date.now() / 1000),
    rdm = parseInt(Math.random() * Math.pow(2, 32)),
    plainText = 'a=' + appid + '&k=' + secretId + '&e=' + (now + pexpired) + '&t=' + now + '&r=' + rdm + userid + '&f=',
    data = new Buffer(plainText, 'utf8'),
    res = crypto.createHmac('sha1', secretKey).update(data).digest(),
    bin = Buffer.concat([res, data]);

//生成base64码
var sign = bin.toString('base64');

// 云函数入口函数
exports.main = async (event, context) => {
    const wxContext = cloud.getWXContext()

    return {
        sign,
        event,
        openid: wxContext.OPENID,
        appid: wxContext.APPID,
        unionid: wxContext.UNIONID,
    }
}